using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TurbaconPhone
{
    public enum BrainState
    {
        
        idle = 0,
        gotoball,
        waitforpass,
        action,
        defend,
        deactivated,
        gotostart,
    };
    
    public class Brain
    {
        float TickTime = 0.25f;
        float tick = 0;
        public BrainState BrainState;
        private Character character;
        private Vector2[] DefendPoints= new Vector2[2];
        private int targetPoint;
        private Character myTeamsforward;
        private Vector2 startPos;
        
        public Brain(Character character)
        {
            targetPoint = 0;
            BrainState = TurbaconPhone.BrainState.idle;
            //BrainState = TurbaconPhone.BrainState.deactivated;
            this.character = character;
            startPos = character.Pos;

            DefendPoints[0] = (character.Team == CharacterTeam.Red) ? new Vector2(100, 480 / 2 + 200) : new Vector2(700, 480 / 2 + 200);
            DefendPoints[1] = (character.Team == CharacterTeam.Red) ? new Vector2(100, 480 / 2 - 100) : new Vector2(700, 480 / 2 - 100);
        }
        public void Update(GameTime gameTime)
        {
            if (myTeamsforward == null)
            {
                foreach (Character ch in TurBacon.GameInfo.Characters)
                {
                    if (ch.Team == character.Team && ch.playerType == PlayerType.forward)
                    {
                        myTeamsforward = ch;
                        break;
                    }
                }
            }

            tick += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (tick >= TickTime)
            {
                tick -= TickTime;
                if (character != TurBacon.GameInfo.CurrentCharacter)
                {
                    switch (BrainState)
                    {
                        case TurbaconPhone.BrainState.idle:
                            switch (character.playerType)
                            {
                                case PlayerType.forward:
                                    BrainState = TurbaconPhone.BrainState.gotoball;
                                    break;
                                case PlayerType.mid:
                                    BrainState = TurbaconPhone.BrainState.waitforpass;
                                    break;
                                case PlayerType.defence:
                                    BrainState = TurbaconPhone.BrainState.defend;
                                    break;
                            }
                            break;

                        case TurbaconPhone.BrainState.gotoball:
                            if ((character.Pos - TurBacon.GameInfo.theBall.Pos).Length() <= TurBacon.kickLength)
                            {
                                BrainState = TurbaconPhone.BrainState.action;
                                Vector2 delta = TurBacon.GameInfo.theBall.Pos - character.Pos;
                                float xSign = character.Team == CharacterTeam.Red ? 1f : -1f;
                                float ySign = Math.Sign(240 - character.Pos.Y);
                                if (Math.Sign(delta.X) != xSign)
                                {
                                    delta.X *= -1.0f;
                                    character.Vel = character.Vel + new Vector2(0, ySign * 100);
                                }
                                TurBacon.GameInfo.theBall.SpecialKick(delta * 100, character);
                                delta.Normalize();
                                

                                character.Kick(delta);
                                break;
                            }

                            character.Move(TurBacon.GameInfo.theBall.Pos);
                            if(character.StopLocation.HasValue)
                                character.TeleportAbility((Vector2)character.StopLocation);
                            break;
                        case TurbaconPhone.BrainState.action:
                            if (character.flying && !character.FullAnim)
                            {
                                character.Dead();
                                break;
                            }
                            if (!character.FullAnim)
                                BrainState = TurbaconPhone.BrainState.idle;
                            break;
                        case TurbaconPhone.BrainState.defend:
                            character.Move(DefendPoints[targetPoint]);
                            if ((character.Pos - DefendPoints[targetPoint]).Length() <= 100)
                            {
                                targetPoint++;
                                targetPoint %= 2;
                            }
                            if ((character.Pos - TurBacon.GameInfo.theBall.Pos).Length() <= 160)
                            {
                                BrainState = TurbaconPhone.BrainState.gotoball;
                            }
                            break;
                        case TurbaconPhone.BrainState.waitforpass:
                            Vector2 target = myTeamsforward.Pos;
                            target.Y = 480 - target.Y;
                            target.X -= ((character.Team == CharacterTeam.Red) ? 200 : -200);
                            if ((character.Pos - myTeamsforward.Pos).Length() > 100)
                            {
                                character.Move(target);
                            }
                            else
                            {
                                character.Stop();
                                character.Face = Character.CharDir.Right;
                            }

                            if ((character.Pos - TurBacon.GameInfo.theBall.Pos).Length() <= 160)
                            {
                                BrainState = TurbaconPhone.BrainState.gotoball;
                            }
                            break;
                    }
                }
            }
        }
    }

}
